home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / Thread.cc < prev    next >
C/C++ Source or Header  |  1990-07-09  |  3KB  |  105 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. // 
  3. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  4. // Copyright (C) 1989 University of Colorado, Boulder, Colorado
  5. // Copyright (C) 1990 University of Colorado, Boulder, Colorado
  6. //
  7. // written by Dirk Grunwald (grunwald@foobar.colorado.edu)
  8. //
  9. #include "CpuMultiplexor.h"
  10. #include "CpuMultiplexorP.h"
  11. #include "Thread.h"
  12. #include "ThreadContainer.h"
  13. #include "assert.h"
  14.  
  15. const void * UNINITIALIZED = 0;
  16.  
  17. Thread::Thread(char* name, unsigned stacksize,
  18.            int checkStack,
  19.            ThreadPriority priority, bool xdebug)
  20.     : (xdebug), pContext(checkStack, stacksize)
  21. {
  22.     threadName = name;
  23.     threadPriority = priority;
  24.     threadState = RUNNABLE;
  25.     cpuAffinity = -1;
  26.  
  27.     pContext.buildReturnFrame(this, voidFuncP(&Thread::startOff));
  28.  
  29.     if (debugFlag) {
  30.     cerr << "[create Thread " << name << " at " << hex(long(this)) << "\n";
  31.     }
  32.     
  33. }
  34.  
  35. Thread::~Thread()
  36. {
  37.     makeTerminated();
  38.     //
  39.     //    The destructor for HardwareContext doesn't free the stack space.
  40.     //  If a thread other than the this thread is calling the destructor,
  41.     //  we need to reclaim that stack space now.
  42.     //
  43.     //  If the current thread is calling the destructor, it must do so
  44.     //  by returning from main & the via the delete statement in startOff.
  45.     //  In that case, we delete the stack in startOff.
  46.     //
  47.     //  It is considered an error for this thread to delete itself in any
  48.     //  other way (i.e. you can do it, but don't complain about what happens).
  49.     //
  50.     if ( this != CurrentThread() ) {
  51.     pContext.reclaimStack();
  52.     }
  53. }
  54.  
  55. void Thread::main()
  56. {
  57.     assert2(FALSE, "[Thread] Subclass of thread forgot to specialize main()");
  58. }
  59.  
  60. void
  61. Thread::startOff()
  62. {
  63.     main();
  64.     void **p = pContext.mallocAt();
  65.     ThisCpu -> threadTerminateException( this );
  66.     assert2(FALSE,"[Thread] Thread exits main");
  67. }
  68.  
  69. void Thread::makeTerminated()
  70. {
  71.     //
  72.     //    this doesn't do anything yet. It's not clear it's needed either,
  73.     //  since most termination-oriented things could happen in the
  74.     //  destructor.
  75.     //
  76. }
  77.  
  78. void Thread::classPrintOn(ostream& strm)
  79. {
  80.     strm << "[Thread:"
  81.      << " " << threadName
  82.          << "  pri: " << int(threadPriority)
  83.          << "  state: ";
  84.     switch (threadState) {
  85.     case SUSPENDED:
  86.     strm << "SUSPENDED";
  87.     break;
  88.     case RUNNING:
  89.     strm << "RUNNING";
  90.     break;
  91.     case RUNNABLE:
  92.     strm << "RUNNABLE";
  93.     break;
  94.     case TERMINATED:
  95.     strm << "TERMINATED";
  96.     break;
  97.  
  98.     case ZOMBIED:
  99.     default: strm << "INVALID";
  100.     break;
  101.     }
  102.     strm << "]\n";
  103.     strm << pContext << "\n";;
  104. }
  105.